package 多线程;

import java.util.concurrent.Semaphore;

public class semaphoreTest {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(3);
        for(int i = 0; i < 10; i++){
            new Thread(() -> {
                try {
                    // 获取信号量，计数 -1
                    semaphore.acquire();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    System.out.println(Thread.currentThread().getName() + "-start...");
                    try {
                        Thread.sleep(1000);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    System.out.println(Thread.currentThread().getName() + "-end...");
                } finally {
                    // 释放信号量，计数 +1
                    semaphore.release();
                }
            }).start();
        }
    }
}
